package com.regent.central.bill.service.bean.stock;

import com.regent.central.bill.core.storehouse.StockChange;
import com.regent.central.bill.dto.delivery.DeliverySendStyleDTO;
import com.regent.central.bill.dto.storehouse.ChangeNoAndNewDateDTO;
import com.regent.central.bill.repository.storehouse.StockChangeMapper;
import com.regent.central.bill.service.storehouse.StockChangeService;
import com.regent.central.infrastructure.common.TimeKit;
import com.regent.central.infrastructure.service.bean.DomainServiceBean;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class StockChangeServiceBean extends DomainServiceBean<StockChangeMapper, StockChange> implements StockChangeService {
    @Override
    public void deliveryOutStock(List<DeliverySendStyleDTO> styles, Boolean insertFlag, String storehouseId, String bossUnitId) {

    }

    @Override
    public ChangeNoAndNewDateDTO getMaxChangeNoAndNewDate(String storehouseId, String date) {
        int no = 1;
        Date newTime = null;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(TimeKit.parse(date, "yyyy-MM-dd"));
        //加一天
        calendar.add(Calendar.DATE, 1);
        String endDate = TimeKit.format(calendar.getTime(), "yyyy-MM-dd");
        StockChange stockChange = this.getLastChange(storehouseId, date, endDate);

        calendar.setTime(TimeKit.parse(date + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
        //取当天 00：00：01
        calendar.add(Calendar.SECOND, 1);
        newTime = calendar.getTime();
        if (stockChange != null) {
            Integer inventoryNo = stockChange.getInventoryNo();
            no = ++inventoryNo;

            Date oldTime = stockChange.getInventoryTime();
            calendar.setTime(oldTime);
            //加1秒
            calendar.add(Calendar.SECOND, 1);
            //变动时间
            newTime =  calendar.getTime();
        }
        ChangeNoAndNewDateDTO changeNoAndNewDateDTO = new ChangeNoAndNewDateDTO();
        changeNoAndNewDateDTO.setNewTime(newTime);
        changeNoAndNewDateDTO.setNo(no);
        return changeNoAndNewDateDTO;
    }

    @Override
    public StockChange getLastChange(String storehouseid, String startDate, String endDate) {
        return mapper.getLastChange(storehouseid, startDate, endDate);
    }
}
